﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Device.Location;

namespace StravaUploader
{
    static public class GPSHelper
    {
        static public LinkedList<GeoPosition<GeoCoordinate>> log;
        static private String getGPXString(LinkedList<GeoPosition<GeoCoordinate>> log)
        {
            String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + Environment.NewLine +
                    "<?xml-stylesheet type=\"text/xsl\" href=\"details.xsl\"?>" + Environment.NewLine +
                    "<gpx" + Environment.NewLine +
                    " version=\"1.1\"" + Environment.NewLine +
                    " creator=\"Unofficial Strava Windows Phone 7 Application\"" + Environment.NewLine +
                    " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + Environment.NewLine +
                    " xmlns=\"http://www.topografix.com/GPX/1/1\"" + Environment.NewLine +
                    " xmlns:topografix=\"http://www.topografix.com/GPX/Private/TopoGrafix/0/1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.topografix.com/GPX/Private/TopoGrafix/0/1 http://www.topografix.com/GPX/Private/TopoGrafix/0/1/topografix.xsd\">" + Environment.NewLine +
                    "<trk>" + Environment.NewLine +
                    "<trkseg>";
            foreach(GeoPosition<GeoCoordinate> i in log)
            {
                content += "<trkpt lat=\"" + i.Location.Latitude + "\" lon=\"" + i.Location.Longitude + "\">" +
                    "<ele>" + i.Location.Altitude + "</ele>" +
                    "<course>" + i.Location.Course + "</course>" +
                    "<speed>" + i.Location.Speed +"</speed>" +
                    "<hdop>" + i.Location.HorizontalAccuracy + "</hdop>" +
                    "<vdop>" + i.Location.VerticalAccuracy + "</vdop>" +
                    "<time>" + i.Timestamp.Year + "-" + i.Timestamp.Month + "-" + i.Timestamp.Day.ToString("D2") +
                            "T" + i.Timestamp.Hour.ToString("D2") + ":" + i.Timestamp.Minute.ToString("D2") + ":" + i.Timestamp.Second.ToString("D2") + "Z</time>" + 
                    "</trkpt>" + Environment.NewLine;
            }
            content += "</trkseg>" + Environment.NewLine +
                        "</trk>" + Environment.NewLine +
                        "</gpx>";

            return content; 
        }

        public static String getGPXString()
        {
            return getGPXString(log);
        }
    }
}
